'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('invitations', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      organization_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '组织ID'
      },
      email: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '被邀请人邮箱'
      },
      role: {
        type: Sequelize.STRING(50),
        comment: '邀请角色'
      },
      token: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
        comment: '邀请令牌'
      },
      status: {
        type: Sequelize.ENUM('pending', 'accepted', 'expired'),
        defaultValue: 'pending',
        comment: '邀请状态'
      },
      expires_at: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '过期时间'
      },
      invited_by: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '邀请人ID'
      },
      created_at: {
        type: Sequelize.DATE
      },
      updated_at: {
        type: Sequelize.DATE
      }
    });

    // 添加索引
    await queryInterface.addIndex('invitations', ['organization_id']);
    await queryInterface.addIndex('invitations', ['token']);
    await queryInterface.addIndex('invitations', ['email']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('invitations');
  }
};